package com.hxx.day5example.service.impl;

import com.hxx.day5example.common.HxxUtils;
import com.hxx.day5example.domain.Emp;
import com.hxx.day5example.service.EmpService;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class EmpServiceImpl implements EmpService {

    //数据库
    private String db = "d:/zmx/db/";
    //属性文件的对象
    private Properties properties = new Properties();

    @Override
    public File findById(Long id) {
        //读取路径
        File file = new File(db);
        //获取所有文件
        File[] files = file.listFiles();
        //判断
        if (files.length>0) {
            //循环
            for (File file1 : files) {
                //加载文件
                try(InputStream inputStream = new FileInputStream(file1);){
                    properties.load(inputStream);
                    //读取内容
                    if (properties.getProperty("id").trim().equals((id+"").trim())) {
                        return file1;
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    @Override
    public void save(Emp emp) {
        if (emp!=null) {
            //把数据添加到属性文件中
            properties.setProperty("id", emp.getId().toString());
            properties.setProperty("username", emp.getUsername());
            properties.setProperty("sex", emp.getSex());
            properties.setProperty("age", emp.getAge().toString());
            //存储到文件中
            try(OutputStream out = new FileOutputStream(db+ HxxUtils.fileName());) {
                //存储
                properties.store(out,"hxx");
                System.out.println("保存成功");
            }catch (Exception e){
                e.printStackTrace();
            }
        }else {
            System.out.println("数据不能为空");
        }
    }

    @Override
    public void update(Emp emp) {
        //获取文件
        if (emp!=null){
            //获取文件
            File file = findById(emp.getId());
            //判断文件是否存在
            if (file!=null) {
                //输出到指定的文件中
                try(OutputStream out = new FileOutputStream(file);) {
                    properties.setProperty("username", emp.getUsername());
                    properties.setProperty("sex", emp.getSex());
                    properties.setProperty("age", emp.getAge().toString());
                    //存储
                    properties.store(out,"hxx");
                    System.out.println("修改成功");
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void delete(Long id) {
        File file = findById(id);
        if (file!=null) {
            file.delete();
            System.out.println("删除成功");
        }
    }

    @Override
    public List<Emp> findAll() {
        List<Emp> list = new ArrayList<>();
        //读取路径
        File file = new File(db);
        //获取所有文件
        File[] files = file.listFiles();
        //判断
        if (files.length>0) {
            for (File file1 : files) {
                //加载
                try(InputStream inputStream = new FileInputStream(file1);){
                    //加載
                    properties.load(inputStream);
                    Emp emp = new Emp();
                    emp.setId(Long.parseLong(properties.getProperty("id")));
                    emp.setUsername(properties.getProperty("username"));
                    emp.setSex(properties.getProperty("sex"));
                    emp.setAge(Integer.parseInt(properties.getProperty("age")));
                    list.add(emp);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        return list;
    }
}
























